var express = require('express');
var router = express.Router();
const goodsController = require('../controllers/goods_controller');
const  goods =require('../models/goods_bean')
router.get('/', function(req, res, next) {
  res.send('respond with a resource');
});

//根据商品id获取数据
router.post('/goodsitem', (req, res) => {
    console.log(req.body.id);
    goodsController.getitem(
        new goods(req.body.id),
        (ret) => {
            res.json(ret);
        },
        (err) => res.json(err)
    );
})


//获取分类数据
router.post('/goodsall', (req, res) => {
    console.log(req.body.id);
    goodsController.getall(
        new goods(req.body.id,req.body.page,req.body.size,req.body.category,req.body.data,req.body.name),
        (ret) =>res.json(ret)
        ,
        (err) => res.json(err)
    );
})

//模糊搜索返回数据
router.post('/selectgoods', (req, res) => {
    console.log(req.body.name);
    goodsController.selectgoods(
        new goods(req.body.id,req.body.page,req.body.size,req.body.category,req.body.data,req.body.name),
        (ret) =>res.json(ret)
        ,
        (err) => res.json(err)
    );
})

//返回所有数据
router.post('/all', (req, res) => {
    goodsController.goodsall(
        new goods(req.body.id,req.body.page,req.body.size,req.body.category,req.body.data,req.body.name),
        (ret) =>res.json(ret)
        ,
        (err) => res.json(err)
    );
})

//根据销量排序返回数据
router.post('/sort', (req, res) => {
    goodsController.sort(
        new goods(req.body.id,req.body.page,req.body.size,req.body.category,req.body.data,req.body.name,req.body.sort),
        (ret) =>res.json(ret)
        ,
        (err) => res.json(err)
    );
})





//根据id删除数据
router.delete('/delete', (req, res) => {
    console.log(req.body.id);
    goodsController.deleteitem(
        new goods(req.body.id),
        (ret) =>res.json(ret)
        ,
        (err) => res.json(err)
    );
})



//添加商品
router.post('/addgoods', (req, res) => {
    goodsController.addgoods(req.body.goods,(ret) =>res.json(ret)
    ,
    (err) => res.json(err)
    )
})


//修改商品
router.put('/updategoods', (req, res) => {
    goodsController.updatitem(new goods(req.body.id,null,null,null,req.body.data),(ret) =>res.json(ret)
    ,
    (err) => res.json(err)
    )
})


module.exports =router
